/**
 * 
 */
package com.googlecode.agiletools.codereview.action;

import com.googlecode.agiletools.codereview.ReviewRecord;
import com.googlecode.agiletools.codereview.ReviewRecordException;
import com.googlecode.agiletools.codereview.ReviewRecordManager;
import com.opensymphony.xwork2.Action;

/**
 * @author Administrator
 * 
 */
public class ReviewRecordAction {

    private ReviewRecord reviewRecord;

    private int recordId;

    /**
     * @param recordId
     *            the recordId to set
     */
    public void setRecordId(int recordId) {
	this.recordId = recordId;
    }

    private ReviewRecordManager reviewRecordManager;

    /**
     * @param reviewRecordManager
     *            the reviewRecordManager to set
     */
    public void setReviewRecordManager(ReviewRecordManager reviewRecordManager) {
	this.reviewRecordManager = reviewRecordManager;
    }

    public String create() {
	return Action.INPUT;
    }

    public String modify() {
	reviewRecord = reviewRecordManager.get(recordId);
	return Action.INPUT;
    }

    public String detail() {
	reviewRecord = reviewRecordManager.get(recordId);
	return Action.SUCCESS;
    }

    public String save() {
	try {
	    validateReviewRecord(reviewRecord);
	    boolean isNew = reviewRecord.getId() == 0;
	    reviewRecord = reviewRecordManager.save(reviewRecord);
	    if (isNew) {
		reviewRecord = null;
		return Action.INPUT;
	    } else {
		return Action.SUCCESS;
	    }
	} catch (ReviewRecordException e) {
	    return Action.ERROR;
	}
    }

    /**
     * @param reviewRecord2
     */
    private void validateReviewRecord(ReviewRecord reviewRecord2) {
	// TODO Validate the review record object.

    }

    /**
     * @param reviewRecord
     *            the reviewRecord to set
     */
    public void setReviewRecord(ReviewRecord reviewRecord) {
	this.reviewRecord = reviewRecord;
    }

    /**
     * @return the reviewRecord
     */
    public ReviewRecord getReviewRecord() {
	return reviewRecord;
    }

}
